import pygame

# 初始化pygame
pygame.init()

# 初始化操纵杆模块
pygame.joystick.init()

# 检查操纵杆数量
joystick_count = pygame.joystick.get_count()
print(f"Number of joysticks: {joystick_count}")

# 如果有操纵杆，创建一个操纵杆对象
if joystick_count > 0:
    joystick = pygame.joystick.Joystick(0)
    joystick.init()  # 初始化操纵杆
    print(f"Joystick name: {joystick.get_name()}")

# 游戏循环
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.JOYBUTTONDOWN:
            print(f"Joystick button {event.button} pressed.")
        elif event.type == pygame.JOYAXISMOTION:
            print(f"Joystick axis {event.axis} value: {event.value}")
        # elif event.type == pygame.JOYHATMOTION:
        #     print(f"Joystick hat {event.hat} value: {event.value}")

# 退出pygame
pygame.quit()
